# heapq堆排序模块

# 场景1 构建拥有权值的对象 并且建立相应的数据结构管理对象-总是取出权值最大的对象
import heapq


class Item(object):

    def __init__(self, name, priority):
        if not isinstance(priority, int):
            raise TypeError('priority Type should be <int>')
        else:
            self.priority = priority

        self.name = name

        self.msg()

    def msg(self):
        print('name: ', self.name, ' priority: ', self.priority)


class PriorityList(object):

    def __init__(self):
        self.ls = list()
        self.index = 0

    def append(self, item):
        heapq.heappush(self.ls, (item.priority, item, self.index))
        self.index += 1

    def pop(self):
        heapq.heappop(self.ls)
        self.index -= 1


if __name__ == '__main__':
    # 创建带有权值的对象
    item1 = Item('laowang', 29)
    item2 = Item('sunxiao', 39)
    # 对象属性创建非整数测试
    # item3 = Item('jiangning', '23')
    item3 = Item('jiangning', 23)

    item_ls = PriorityList()

    item_ls.append(item1)
    item_ls.append(item2)
    item_ls.append(item3)
    print(item_ls.index)

    item_ls.pop()

    print(item_ls.index)

